<?php

class loginValidator extends sfValidator
{

  public function initialize($context, $parameters = null)
  {
    // initialize parent
    parent::initialize($context);

    // set defaults
    $this->setParameter('login_error', 'Saisie incorrecte');

    $this->getParameterHolder()->add($parameters);

    return true;
  }

  public function execute(&$value, &$error)
  {
    $password_param = $this->getParameter('password');
    $password = $this->getContext()->getRequest()->getParameter($password_param);

    $login = $value;

    $c = new Criteria();
    $c->add(GuardUserPeer::LOGIN, $login);
    $user = GuardUserPeer::doSelectOne($c);

    // login exist ?
    if ($user)
    {
      // password OK ?
      if (sha1($user->getSalt().$password) == $user->getSha1Password())
      {
        $remember = $this->getContext()->getRequest()->getParameter('remember_me');
        $this->getContext()->getUser()->signIn($user, $remember);
        return true;
      }

    }

    $error = $this->getParameter('login_error');
    return false;

  }




}
